package com.uinnova.product.eam.mvc;

import com.alibaba.fastjson.JSON;
import com.binary.framework.web.RemoteResult;
import com.binary.jdbc.Page;
import com.uinnova.product.eam.dto.SimpPlanInfo;
import com.uinnova.product.eam.service.ArchReviewService;
import com.uinnova.product.eam.vo.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/arch/review")
public class ArchReviewController {

    @Autowired
    private ArchReviewService archReviewService;

    @PostMapping("/submit/before/info")
    public RemoteResult submitBeforeInfo(@RequestBody String body) {
        if (StringUtils.isBlank(body)) {
            return new RemoteResult(new ArchReviewSubmitBeforeInfo());
        }
        Long planId = JSON.parseObject(body).getLong("planId");
        ArchReviewSubmitBeforeInfo submitBeforeInfo = archReviewService.submitBeforeInfo(planId);
        return new RemoteResult(submitBeforeInfo);
    }

    @PostMapping("/submit")
    public RemoteResult submit(@RequestBody ArchReviewSubmitReq req) {
        ArchReviewSubmitRes res = archReviewService.submit(req);
        return new RemoteResult(res);
    }

    @PostMapping("/reviewPlans")
    public RemoteResult reviewPlans(@RequestBody ReviewPlanReq req) {
        Page<SimpPlanInfo> page = archReviewService.reviewPlans(req);
        return new RemoteResult(page);
    }

    @GetMapping("/approveInfo")
    public RemoteResult approveInfo(@RequestParam String taskId) {
        ArchReviewApproveInfo approveInfo = archReviewService.approveInfo(taskId);
        return new RemoteResult(approveInfo);
    }

    @PostMapping("/list")
    public RemoteResult approveInfo(@RequestBody ArchReviewQueryReq req) {
        Page<ArchReviewRes> page = archReviewService.page(req);
        return new RemoteResult(page);
    }

    @GetMapping("/getState")
    public RemoteResult getState(@RequestParam String processInstanceId) {
        ArchReviewRes result = archReviewService.getState(processInstanceId);
        return new RemoteResult(result);
    }

    @GetMapping("/approveInfo/{id}")
    public RemoteResult approveInfo(@PathVariable Long id) {
        ArchReviewApproveInfo approveInfo = archReviewService.approveInfo(id);
        return new RemoteResult(approveInfo);
    }

    @PostMapping("/circulate")
    public RemoteResult circulate(@RequestBody CirculateReq req) {
        archReviewService.circulate(req);
        return new RemoteResult("success");
    }

    @PostMapping("/cirCulateList")
    public RemoteResult circulate(@RequestBody CirculateListReq req) {
        Page<CirculateListRes> page = archReviewService.cirCulateList(req);
        return new RemoteResult(page);
    }

    @GetMapping("/finishCirculateJob/{workbenchChargeDoneId}")
    public RemoteResult circulate(@PathVariable Long workbenchChargeDoneId) {
        archReviewService.finishCirculateJob(workbenchChargeDoneId);
        return new RemoteResult("success");
    }
}
